package com.wt.struggle.goodbye.user.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.os.Message
import android.support.design.widget.TabLayout
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import android.view.Gravity
import android.widget.TextView
import com.google.gson.reflect.TypeToken
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.Config
import com.wt.struggle.goodbye.base.ProActivity
import com.wt.struggle.goodbye.base.Send
import com.wt.struggle.goodbye.info.NetInfo
import com.wt.struggle.goodbye.info.NewsInfo
import com.wt.struggle.goodbye.main.activity.ShareActivity
import com.wt.struggle.goodbye.sq.adapter.PickUserAdapter
import kotlinx.android.synthetic.main.head.*
import kotlinx.android.synthetic.main.scan_history_layout.*
import java.util.ArrayList

/**
 * 浏览记录
 */
class ScanHistoryActivity : ProActivity() {

    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {

        when (what) {
            Send.SCAN_RECORD_CODE -> {

                removeLoadDialog()
                swipeRefreshView.isRefreshing = false

                if (netInfo.code == Config.SUCCESS) {
                    val result = netInfo.dataResult
                    if (result != null && result != "" && result != "null") {
                        val arr = gson!!.fromJson<ArrayList<NewsInfo>>(
                            result,
                            object : TypeToken<ArrayList<NewsInfo>>() {}.type
                        )

                        if (arr.size != 0) {
                            adapter.updateDataClear(arr)
                        } else {
                            showToastShort("暂无数据")
                        }

                    }
                }
            }
        }
    }


    val tabList = arrayListOf<String>("全部", "本周", "今天")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.scan_history_layout)
        tvTitle.text = "浏览历史"
        imageBack.setOnClickListener {
            finish()
        }

        initAdapter()
        initTabLayout()

        getScanList()

        swipeRefreshView.setOnRefreshListener {
            page = 1
            getScanList()
        }
    }

    var page = 1
    var type = 1

    private fun getScanList() {
        send!!.scanRecordList(page, type, handler)
        showLoadDialog("获取中")
    }

    private fun initTabLayout() {
        for (i in tabList.indices) {
            val textView = TextView(this)
            textView.gravity = Gravity.CENTER
            changeTextStyle(textView, i == 0)
            textView.text = tabList[i]
            scanTabLayout.addTab(scanTabLayout.newTab().setCustomView(textView))

        }

        scanTabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                val position = tab!!.position
                change(tabList, position)

                when (tab.position) {
                    0 -> type = 1
                    1 -> type = 2
                    2 -> type = 3
                }

                getScanList()
            }

        })
    }


    /**
     * 循环改变样式
     */
    fun change(tabLayoutList: ArrayList<String>, position: Int) {
        for (i in tabLayoutList.indices) {
            val textView = scanTabLayout.getTabAt(i)!!.customView as TextView
            changeTextStyle(textView, i == position)
        }
    }


    val list = arrayListOf<NewsInfo>()
    lateinit var adapter: PickUserAdapter

    private fun initAdapter() {

        historyRecyclerView.layoutManager = LinearLayoutManager(this)
        historyRecyclerView.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
        historyRecyclerView.setHasFixedSize(true)

        adapter = PickUserAdapter(this, list)
        historyRecyclerView.adapter = adapter

        adapter.onFocusOnClickListener = object : PickUserAdapter.OnFocusOnClickListener {

            override fun onFocusOn(position: Int) {
            }

            override fun onCollect(position: Int) {
            }

            override fun onZan(position: Int) {
            }

            override fun onDownZan(position: Int) {
                val intent = Intent(this@ScanHistoryActivity, SiXinOrRecoverActivity::class.java)
                intent.putExtra("code", 1)
                intent.putExtra("user", list[position].userExtraInfo)
                startActivity(intent)
            }

            override fun onComment(position: Int) {

            }

            override fun onMore(position: Int) {

                this@ScanHistoryActivity.indexPosition = position
                val intent = Intent(this@ScanHistoryActivity, ShareActivity::class.java)
                intent.putExtra("code", 3)
                val info = list[position]
                intent.putExtra("info", info)
                intent.putParcelableArrayListExtra("videoList", info.videoList)
                intent.putParcelableArrayListExtra("voiceList", info.audioList)

                startActivityForResult(intent, 4567)
            }

        }
    }

    var indexPosition = 0


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                4567 -> {
                    list.removeAt(indexPosition)
                    adapter.notifyItemRemoved(indexPosition)
                }

            }
        }
    }
}